﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace CoreLib.MySQL
{
    public class DbDelete : DbBaseQuery<DbDelete>, IDbQuery, IDisposable
    {
        private DbProvider dbProvider;
        public DbDelete(DbProvider dbProvider)
        {
            base.TQuery = this;
            this.dbProvider = dbProvider;
        }

        public DbDelete From(string table)
        {
            base.table = table;
            return this;
        }

        public new DbDelete Where(string column, Comparisson comparer, object value)
        {
            return base.Where(column, comparer, value);
        }

        public new DbDelete And(string column, Comparisson comparer, object value)
        {
            return base.And(column, comparer, value);
        }

        public new DbDelete Or(string column, Comparisson comparer, object value)
        {
            return base.Or(column, comparer, value);
        }

        public new DbDelete Limit(int limit)
        {
            return base.Limit(limit);
        }

        public void Execute()
        {
            StringBuilder queryBuilder = new StringBuilder();
            queryBuilder.AppendFormat("Delete From {0} <W>", base.table);
            if (limit != 0) queryBuilder.AppendFormat(" Limit {0}", base.limit);
            using (MySqlCommand cmd = new MySqlCommand())
            {
#if UNMANAGED
                string[] locations = new string[base.whereClauses.Count];
                for (int i = 0; i < locations.Length; i++)
                {
                    string param = base.GetParameter();
                    QuerySegment* segment = (QuerySegment*)base.whereClauses.Dequeue();
                    cmd.Parameters.AddWithValue(param, new string(segment->Value));
                    locations[i] = string.Format("{0} {1} {2} {3}", segment->Connector.ToString(), new string(segment->Column), TranslateOperator(segment->Comparisson), param);
                    Marshal.FreeHGlobal((IntPtr)segment);
                }
                queryBuilder.Replace("<W>", string.Join(" ", locations));
#else
                string[] locations = new string[base.whereClauses.Count];
                for (int i = 0; i < locations.Length; i++)
                {
                    string param = base.GetParameter();
                    QuerySegment segment = (QuerySegment)base.whereClauses.Dequeue();
                    cmd.Parameters.AddWithValue(param, segment.Value);
                    locations[i] = string.Format("{0} {1} {2} {3}", segment.Connector.ToString(), segment.Column, TranslateOperator(segment.Comparisson), param);
                }
                queryBuilder.Replace("<W>", string.Join(" ", locations));
#endif
                using (cmd.Connection = dbProvider.CreateDbConnection())
                {
                    cmd.CommandText = queryBuilder.ToString();
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public void Dispose()
        {
            //TODO
        }
    }
}
